import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import store from '../store'

Vue.use(VueRouter)

let router = new VueRouter({
    routes
    
})

// 全局前置守卫
const whiteList=['/login']
router.beforeEach((to,from,next)=>{
    if(to.path==='/login') return next()
    const token = store.state.login.token
    if(!token) return next('/login')
    if(store.state.login.token){
        if(to.path==='/login'){
            next('/')
        }else{
            next()
        }
    }else{
        if(whiteList.includes(to.path)){
            next()
        }else{
            next('/login')
        }
    }
})

export default router
